package com.dwd.quartz.job;

import com.dwd.basic.constant.PayConstants;
import com.dwd.order.mapper.ProductOrderMapper;
import com.dwd.order.pojo.ProductOrder;
import com.dwd.pay.mapper.PayBillMapper;
import com.dwd.pay.pojo.PayBill;
import com.dwd.quartz.pojo.QuartzJobInfo;
import com.dwd.servicemanager.mapper.ProductMapper;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/30 11:15
 * @Version 1.0
 * 15分钟之后执行任务，修改状态
 */
public class OrderJob implements Job {
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private PayBillMapper payBillMapper;

    /**
     * 要执行的任务
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //获取jobdata
        JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
        //获取参数
        QuartzJobInfo quartzJobInfo = (QuartzJobInfo) jobDataMap.get("params");

        //获取类型，根据不同类型进行操作
        switch (quartzJobInfo.getType()) {
            case PayConstants.BUSINESSTYPE_PRODUCT:
                //获取设置的ordersn
                Map<String, Object> params = quartzJobInfo.getParams();
                String orderSn = (String) params.get("OrderSn");
                //查询唯一数据
                ProductOrder productOrder = productOrderMapper.queryByOrderSn(orderSn);
                //修改osrder_product 状态-1 支付取消
                productOrder.setState(PayConstants.PAY_CANCLE)
                        .setLastConfirmTime(new Date());
                productOrderMapper.update(productOrder);
                //修改paybill
                PayBill payBill = payBillMapper.queryByorderSn(orderSn);
                payBill.setState(PayConstants.PAY_CANCLE)
                        .setUpdateTime(new Date());
                payBillMapper.update(payBill);
                break;
            case PayConstants.BUSINESSTYPE_ADOPT:
                break;
            case PayConstants.BUSINESSTYPE_GOODS:
                break;
            default:
        }
    }
}
